package com.kha.listone.util.xml;

import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import org.kxml2.io.KXmlSerializer;
import org.kxml2.kdom.Document;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

import com.kha.listone.common.model.TaskModel;
import com.kha.listone.util.Globals;
import com.kha.listone.util.operation.Operation;

public class TasksXMLGenerator implements Operation {

	public TasksXMLGenerator() {
		super();
	}

	public void createDocument() {

		if (Globals.taskList != null || Globals.taskList.size() > 0) {

			try {
				FileConnection fileConn = (FileConnection) Connector.open(
						Globals.XML_TASKLIST_FILE_PATH, Connector.READ_WRITE);
				if (fileConn.exists()) {
					fileConn.delete();
				}
				fileConn.create();
				OutputStream outStream = fileConn.openOutputStream();

				// Create the new xml file for
				Document doc = new Document();

				Element root = doc.createElement("", "parent");
				root.setName(Globals.TAG_TASKLIST);

				for (int i = Globals.taskList.size() - 1; i >= 0; i--) {

					TaskModel model = (TaskModel) Globals.taskList.elementAt(i);

					Element task = doc.createElement("", "child");
					task.setName(Globals.TAG_TASK);

					// Task Name
					Element taskName = doc.createElement("", "child");
					taskName.setName(Globals.TAG_TASKNAME);
					taskName.addChild(0, Node.TEXT, model.getTaskName());
					task.addChild(0, Node.ELEMENT, taskName);

					// Task Note
					Element taskNote = doc.createElement("", "child");
					taskNote.setName(Globals.TAG_TASKNOTE);
					taskNote.addChild(0, Node.TEXT, model.getTaskNote());
					task.addChild(0, Node.ELEMENT, taskNote);

					// Task Recurrence
					Element taskRecurrence = doc.createElement("", "child");
					taskRecurrence.setName(Globals.TAG_TASKRECURRENCE);
					taskRecurrence.addChild(0, Node.TEXT,
							String.valueOf(model.getRecurrenceStatus()));
					task.addChild(0, Node.ELEMENT, taskRecurrence);

					// Task Tags
					Element taskTags = doc.createElement("", "child");
					taskTags.setName(Globals.TAG_TASKTAG);
					taskTags.addChild(0, Node.TEXT,
							String.valueOf(model.getTaskTags()));
					task.addChild(0, Node.ELEMENT, taskTags);

					// Task Important
					Element taskImportant = doc.createElement("", "child");
					taskImportant.setName(Globals.TAG_TASKIMPORTANT);
					taskImportant.addChild(0, Node.TEXT,
							String.valueOf(model.getImportantRate()));
					task.addChild(0, Node.ELEMENT, taskImportant);

					// Task DueDate
					Element taskDueDate = doc.createElement("", "child");
					taskDueDate.setName(Globals.TAG_TASKDUE);
					taskDueDate.addChild(0, Node.TEXT,
							String.valueOf(model.getDueDate().getTime()));
					task.addChild(0, Node.ELEMENT, taskDueDate);

					// Task DueDate
					Element taskDeadlineDate = doc.createElement("", "child");
					taskDeadlineDate.setName(Globals.TAG_TASKDEADLINE);
					taskDeadlineDate.addChild(0, Node.TEXT,
							String.valueOf(model.getDeadlineDate().getTime()));
					task.addChild(0, Node.ELEMENT, taskDeadlineDate);

					// Task Date Recurrence
					Element taskDateRecurrence = doc.createElement("", "child");
					taskDateRecurrence.setName(Globals.TAG_TASKIMPORTANT);
					taskDateRecurrence.addChild(0, Node.TEXT,
							model.getDateRecurrence());
					task.addChild(0, Node.ELEMENT, taskDateRecurrence);

					// Task Done
					Element taskDone = doc.createElement("", "child");
					taskDone.setName(Globals.TAG_TASKDONE);
					taskDone.addChild(0, Node.TEXT,
							String.valueOf(model.getDone()));
					task.addChild(0, Node.ELEMENT, taskDone);

					root.addChild(0, Node.ELEMENT, task);
				}

				doc.addChild(0, Node.ELEMENT, root);

				// Write into the serialization
				KXmlSerializer serializer = new KXmlSerializer();
				serializer.setOutput(outStream, "UTF-8");

				doc.write(serializer);
				outStream.close();
				fileConn.close();

			} catch (IOException e) {

				e.printStackTrace();
			}
		}
	}

	public void execute() {
		this.createDocument();
	}

}
